package com.test.clyq.daoImpl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.dao.DataAccessException;

import com.test.clyq.dao.DataPermissionDao;
import com.test.clyq.entity.DataPermission;
import com.test.clyq.entity.Users;
import com.test.dao.hibernate.BaseDaoHibernate;
import com.test.util.FormatExceptionStack;
import com.test.util.SessionFactoryUtil;

/**
 * DataPermissionDaoHibernate. <br>
 * 数据权限信息管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:37:08
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class DataPermissionDaoHibernate extends BaseDaoHibernate implements DataPermissionDao {

	public void addDataPermission(DataPermission permissin, Users user) {
		this.log.info("新增数据权限start");
		try {
			saveOrUpdateItem(permissin, user);
			this.log.debug("新增数据权限成功");
			this.log.info("新增数据权限end");
		}
		catch (DataAccessException e) {
			this.log.error("[DataPermission]addDataPermission发生错误！\n" + FormatExceptionStack.formatException(e));
		}
	}

	public boolean delDataPermissionById(long id, Users user) {
		log.info("根据模板栏目对应ID删除模板栏目对应start");
		try {
			DataPermission dataPermission = (DataPermission) getItem(DataPermission.class, id);
			if (dataPermission != null) {
				removeItem(DataPermission.class, id, user);
				log.debug("删除模板栏目对应信息成功");
				log.info("根据模板栏目对应ID删除模板栏目对应end");
				return true;
			} else {
				log.debug("删除模板栏目对应信息失败");
				return false;
			}
		}
		catch (DataAccessException e) {
			log.error("[DataPermissionDaoHibernate]delDataPermissionById发生错误！\n" + FormatExceptionStack.formatException(e));
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public Set<DataPermission> getDatePermissionByUserIdAndCategoryId(Long categoryId, Long userId) {
		log.info("获得列表start");
		StringBuilder hsql = new StringBuilder();
		hsql.append("from DataPermission ");
		if (userId != null) {
			hsql.append(" where datePermissionUserId=" + userId);
		}
		if (categoryId != null) {
			if (userId != null) {
				hsql.append(" and datePermissionCategoryId=" + categoryId);
			} else {
				hsql.append(" where datePermissionCategoryId=" + categoryId);
			}
		}
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(hsql.toString());
			List<DataPermission> list = query.list();
			Set<DataPermission> dataPermissionSet = null;
			if (list != null && list.size() > 0) {
				dataPermissionSet = new HashSet<DataPermission>();
				log.debug("获得数据");
				log.info("获得end");
				for (DataPermission permission : list) {
					dataPermissionSet.add(permission);
				}
				return dataPermissionSet;

			} else {
				log.debug("表中没有数据");
				return null;
			}
		}
		catch (DataAccessException e) {
			log.error("[List<DataPermissionDaoHibernate>]getDatePermissionByUserIdAndCategoryId发生错误！\n" + FormatExceptionStack.formatException(e));
			return null;
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}
}
